package com.xhtech.base.service.user

import com.xhtech.base.data.AppLoginData
import com.xhtech.base.service.AppService
import com.xhtech.base.service.login.LoginManager
import com.xhtech.base.service.user.cache.IUserCache
import com.xhtech.base.service.user.cache.UserCacheImpl
import com.xhtech.base.service.user.login.ILoginInfo
import com.xhtech.base.service.user.login.UserLoginInfo
import com.xhtech.base.service.user.monitor.IUserMonitorListener
import com.xhtech.base.service.user.monitor.UserMonitorHelp


/**
 * @author yuanxh
 * @date :2023/6/16 20:11
 * @description: 用户管理
 */
class UserInfoManager private constructor() : IUserCache by mIUserCache, ILoginInfo by mIUserLogin,
    IUserMonitorListener by mMonitorHelp {

    var loginInfo: AppLoginData? = null

    fun initLogin(type: UserType, appLoginData: AppLoginData) {
        setLoginType(type.type)
        loginInfo = appLoginData
        saveUserType(type.type)
        saveToken(appLoginData.token)
        saveUserId(appLoginData.userId)
        savePhone(appLoginData.phone)
        loginComplete()
    }

    fun initTokenLogin() {
        setLoginType(getSaveUserType())
        loginComplete()
    }


    fun isOnlyUser(userId: String?): Boolean {
        return !userId.isNullOrEmpty() && getSaveUserId().equals(userId)
    }

    //登录完成
    private fun loginComplete() {
        LoginManager.getInstances().onLoginComplete()
        AppService.get().loadLoginUserInfo()
    }


    companion object {

        private val instance by lazy { UserInfoManager() }

        private val mIUserCache: IUserCache = UserCacheImpl()
        private val mIUserLogin: ILoginInfo = UserLoginInfo()
        private val mMonitorHelp: IUserMonitorListener = UserMonitorHelp()

        @JvmStatic
        fun getInstances() = instance


    }
}